package com.example.demo.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @version 1.0
 * @Author: LJP
 * @Description:      mybatisPlus的配置实现数据库时间的自动刚更新
 * 实体类字段要加上@TableField(fill = FieldFill.xxx)该配置才能生效
 * @Date: Created in 11:51 2021/12/2
 */
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        this.setFieldValByName("createdAt", LocalDateTime.now(),metaObject);
        this.setFieldValByName("updatedAt", LocalDateTime.now(),metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updatedAt", LocalDateTime.now(),metaObject);
    }
}
